<script setup lang="ts">
const username = ref<string>('')
onMounted(function () {
  cFetch<string>('/admin/verify-token', {
    auth: true,
    defaultOpts: {
      method: "GET"
    }
  }).then(res => {
    if (res.code === 200) {
      username.value = res.data
    }
  })
})

function exit() {
  userStore().setToken('')
  navigateTo('/login')
}

const open = () => {
  ElMessageBox.prompt(
      '输入新密码：',
      '修改密码',
      {
        confirmButtonText: '确认修改',
        cancelButtonText: '取消',
        type: 'warning',
        center: true,
        inputPattern: /^[A-Za-z0-9]+$/,
        inputErrorMessage: '只允许数字和字母的组合',
        inputType: 'password'
      }
  )
      .then(({value}) => {
        cFetch('/admin/modify-password', {
          auth: true,
          defaultOpts: {
            method: "POST",
            body: {
              password: value
            }
          }
        }).then(res => {
          if (res.code === 200) {
            ElMessage({
              type: 'success',
              message: '修改成功',
            })
          }
        })
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '取消修改',
        })
      })
}
</script>

<template>
  <div>
    <el-container>
      <el-aside class="aside">
        <el-scrollbar>
          <div class="title">
            <span style="font-size: 1.3rem;">
            顺风车后台管理系统
            </span>
          </div>
          <el-menu
              :default-openeds="['1']"
              default-active="1-1"
          >
            <el-sub-menu index="1">

              <template #title>
                <el-icon>
                  <ElIconUser/>
                </el-icon>
                用户管理
              </template>

              <el-menu-item-group>
                <template #title>用户信息管理</template>
                <el-menu-item index="1-1" @click="navigateTo('/')">用户列表</el-menu-item>
                <el-menu-item index="1-2" @click="navigateTo('/increase')">新增用户</el-menu-item>
              </el-menu-item-group>

              <el-menu-item-group title="在线用户管理">
                <el-menu-item index="1-3" @click="navigateTo('/online')">在线用户列表</el-menu-item>
              </el-menu-item-group>
            </el-sub-menu>

            <el-sub-menu index="2">
              <template #title>

                <el-icon>
                  <ElIconAvatar/>
                </el-icon>
                管理员信息
              </template>

              <el-menu-item-group>
                <template #title>管理员信息管理</template>
                <el-menu-item index="2-1" @click="navigateTo('/admin')">管理员列表</el-menu-item>
                <el-menu-item index="2-2" @click="navigateTo('/admin/increase')">添加管理员</el-menu-item>
              </el-menu-item-group>
            </el-sub-menu>

            <el-sub-menu index="3">
              <template #title>
                <el-icon>
                  <ElIconPieChart/>
                </el-icon>
                统计信息
              </template>
              <el-menu-item-group>
                <template #title>统计数据</template>
                <el-menu-item index="3-1" @click="navigateTo('/statistics')">角色数据详情</el-menu-item>
                <el-menu-item index="3-2" @click="navigateTo('/order')">订单数据详情</el-menu-item>
                <el-menu-item index="3-3" @click="navigateTo('/stroke')">行程数据详情</el-menu-item>
              </el-menu-item-group>
            </el-sub-menu>

            <el-sub-menu index="4">
              <template #title>
                <el-icon>
                  <ElIconSetting/>
                </el-icon>
                系统信息
              </template>

              <el-menu-item-group>
                <template #title>静态信息查看</template>
                <el-menu-item index="4-1" @click="navigateTo('/sta')">系统信息</el-menu-item>
              </el-menu-item-group>

              <el-menu-item-group title="动态系统信息">
                <el-menu-item index="4-2" @click="navigateTo('/dyn')">查看详情</el-menu-item>
              </el-menu-item-group>

            </el-sub-menu>
          </el-menu>

        </el-scrollbar>

      </el-aside>

      <el-container>
        <el-header class="header">
          <el-text type="info">数据看板</el-text>
          <div class="toolbar">
            <span>管理员：</span>
            <el-dropdown>
              <el-icon size="large" style="margin-right: 8px; margin-top: 1px">
                <ElIconSetting/>
              </el-icon>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item @click="open">修改密码</el-dropdown-item>
                  <el-dropdown-item @click="exit">退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
            <span>{{ username }}</span>
          </div>
        </el-header>

        <el-main>
          <slot name="default"></slot>
        </el-main>

      </el-container>

    </el-container>
  </div>
</template>

<style scoped lang="scss">

.aside {
  height: 98vh;
  width: 20vw;
  overflow: auto;
  background:linear-gradient( to top  ,white,#d6d0d0 ) ;
  border-radius: 10px 0 0 10px;
}

.title {
  height: 4rem;
  display: flex;
  justify-content: center;
  align-items: center;
  font-family: 华文楷体, serif;
  font-weight: bold;
  background: linear-gradient(45deg,white,#32c7f3);
  position: sticky;
  top: 0;
  z-index: 1;
  border-radius: 10px 0 0 0;
  border: 1px outset gray;
}

.header {
  background: linear-gradient(to right , #fffbfb, #ffbf58) ;
  border-radius: 0 10px 0 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

</style>